import { DataTypes, Model } from 'sequelize'
import { sequelize } from '../../connect'
import { findAllValues, findOneRandomValue, findOneValue } from '../../public'

type ModelProps = {
  id: number // 信件ID
  uid: number // 用户ID
  from: string // 发件人
  title: string // 标题
  content: string // 内容
  things: string // 物品列表
  create_time: Date // 创建时间
  read_time: Date // 阅读时间
  get_time: Date // 领取时间
  finish_time: Date // 过期时间
  status: number // 0: 未读, 1: 已读, 2: 已获取, 3: 已删除
}

class letters extends Model<ModelProps> {
  public static findAllValues = findAllValues
  public static findOneValue = findOneValue
  public static findOneRandomValue = findOneRandomValue
}

letters.init(
  {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      allowNull: false
    },
    uid: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '用户'
    },
    from: {
      type: DataTypes.STRING(20),
      allowNull: false,
      comment: '发件人',
      defaultValue: '未知'
    },
    title: {
      type: DataTypes.TEXT,
      allowNull: false,
      comment: '标题',
      defaultValue: '无标题'
    },
    content: {
      type: DataTypes.TEXT,
      allowNull: false,
      comment: '内容',
      defaultValue: '无内容'
    },
    things: {
      type: DataTypes.STRING(255),
      allowNull: false,
      comment: '物品列表',
      defaultValue: '无'
    },
    create_time: {
      type: DataTypes.DATE,
      allowNull: false,
      comment: '创建时间'
    },
    read_time: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: '阅读时间'
    },
    get_time: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: '领取时间'
    },
    finish_time: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: '完成时间'
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '状态',
      defaultValue: 0 // 默认未读
    }
  },
  {
    sequelize,
    tableName: 'letters',
    freezeTableName: true,
    createdAt: false,
    updatedAt: false,
    indexes: [
      {
        unique: true,
        fields: ['id']
      }
    ]
  }
)

export { letters }
